#!/usr/bin/env python
# coding=utf-8
# Cron Job 列表

import wsgiref.handlers
from google.appengine.ext import webapp
from google.appengine.api import users
from google.appengine.ext.webapp import util
import crondb
import common

class AjaxHandler(webapp.RequestHandler):
	def process(self):
		is_ajax = self.request.get('is_ajax')
		act = self.request.get('act')
		error = 0
		msg = ""
		content = ""
		user = users.get_current_user()
		if user:
			if users.is_current_user_admin():
				if act == "ajax_edit_title":
					id = self.request.get('id')
					if id:
						id = int(id)
					else:
						id = 0
					if id > 0:
						cron = crondb.getCronInfo(id)
					else:
						cron = False;
					if cron:
						org = self.request.get('org')
						val = self.request.get('val')
						cron.title = val
						crondb.updateCronInfo(cron)
						error = 0
						msg = ""
						content = val
					else:
						error = 1
						msg = "Can not found cron job in database. ID=%d" % id
				elif act == "ajax_edit_url":
					id = self.request.get('id')
					if id:
						id = int(id)
					else:
						id = 0
					if id > 0:
						cron = crondb.getCronInfo(id)
					else:
						cron = False;
					if cron:
						org = self.request.get('org')
						val = self.request.get('val')
						cron.url = val
						crondb.updateCronInfo(cron)
						error = 0
						msg = ""
						content = val
					else:
						error = 1
						msg = "Can not found cron job in database. ID=%d" % id
				elif act == "ajax_edit_sendokmsg":
					id = self.request.get('id')
					if id:
						id = int(id)
					else:
						id = 0
					if id > 0:
						cron = crondb.getCronInfo(id)
					else:
						cron = False;
					if cron:
						org = self.request.get('org')
						val = self.request.get('val')
						cron.sendSuccessMsg = (val == "1")
						crondb.updateCronInfo(cron)
						error = 0
						msg = ""
						if cron.sendSuccessMsg:
							content = '1'
						else:
							content = '0'
					else:
						error = 1
						msg = "Can not found cron job in database. ID=%d" % id
				elif act == "ajax_edit_sendfailmsg":
					id = self.request.get('id')
					if id:
						id = int(id)
					else:
						id = 0
					if id > 0:
						cron = crondb.getCronInfo(id)
					else:
						cron = False;
					if cron:
						org = self.request.get('org')
						val = self.request.get('val')
						cron.sendFailMsg = (val == "1")
						crondb.updateCronInfo(cron)
						error = 0
						msg = ""
						if cron.sendFailMsg:
							content = '1'
						else:
							content = '0'
					else:
						error = 1
						msg = "Can not found cron job in database. ID=%d" % id
				elif act == "ajax_edit_enabled":
					id = self.request.get('id')
					if id:
						id = int(id)
					else:
						id = 0
					if id > 0:
						cron = crondb.getCronInfo(id)
					else:
						cron = False;
					if cron:
						org = self.request.get('org')
						val = self.request.get('val')
						cron.enabled = (val == "1")
						crondb.updateCronInfo(cron)
						error = 0
						msg = ""
						if cron.enabled:
							content = '1'
						else:
							content = '0'
					else:
						error = 1
						msg = "Can not found cron job in database. ID=%d" % id
				else:
					error = 1
					msg = "No action."
					content = ""
				
				self.response.headers["Content-Type"] = "text/plain; charset=utf-8"
				self.response.out.write("""{ "error": %d, "message": "%s", "content": "%s" }""" % (error, common.encodeJsString(msg), common.encodeJsString(content)))
			else:
				error = 1
				msg = "You are no permission."
				content = ""
				self.response.headers["Content-Type"] = "text/plain; charset=utf-8"
				self.response.out.write("""{ "error": %d, "message": "%s", "content": "%s" }""" % (error, common.encodeJsString(msg), common.encodeJsString(content)))
		else:
			error = 1
			msg = "The session is time out."
			content = ""
			self.response.headers["Content-Type"] = "text/plain; charset=utf-8"
			self.response.out.write("""{ "error": %d, "message": "%s", "content": "%s" }""" % (error, common.encodeJsString(msg), common.encodeJsString(content)))

	def get(self):
		self.process()

	def post(self):
		self.process()

def main():
	application = webapp.WSGIApplication([('/ajax', AjaxHandler)],
		debug=True)
	util.run_wsgi_app(application)


if __name__ == '__main__':
	main()
